<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/10/16
 * Time: 下午5:52
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class  Sharem extends CI_Model
{
    const RESOUCE_TYPE_POST         = 0; // 帖子类型
    const RESOUCE_TYPE_DISUBJECT    = 1; // 专题类型
    const RESOUCE_TYPE_PART         = 2; // 图谱类型
    const RESOUCE_TYPE_INVITE_REG   = 3; // 邀请注册类型
    const RESOUCE_TYPE_H5           = 4; // H5活动类型

    public function insert_share_action($uid,$type,$share_id,$channel)
    {
        $addtime = time();
        $query = array("user_id"=>$uid,"addtime"=>$addtime, "type"=>$type, "share_id"=>$share_id, "channel"=>$channel);
        return $this->db->insert("ml_share_log",$query);
    }
    //查询当天已经分享数 同一渠道算一次
    public function share_cout_bydate($user_id,$begin_time, $end_time,$type)
    {
        /*$where = array();
        $where['addtime > '] = $begin_time;
        $where['addtime < '] = $end_time;
        $where['user_id'] = $user_id;
        $where['type'] = $type;

        $this->db->select('count')->distinct();
        $count = $this->db->from("ml_share_log")->where($where)->count_all_results();*/

        $sql = 'SELECT COUNT(a.count) AS `numrows`
FROM (
SELECT count(id) as count
FROM `ml_share_log`
WHERE `addtime` > '.$begin_time.'
AND `addtime` < '.$end_time.'
AND `user_id` = '.$user_id.'
AND `type` ='.$type.'
GROUP BY `channel`,`share_id`
)  as a
';
        $count = $this->db->query($sql)->result_array();
        return $count[0]['numrows'];
    }

    //查询当天已经分享数  同一渠道
    public function share_cout_bydate_with_channel($user_id,$begin_time, $end_time,$type,$share_id,$channel)
    {
        $where = array();
        $where['addtime > '] = $begin_time;
        $where['addtime < '] = $end_time;
        $where['user_id'] = $user_id;
        $where['share_id'] = $share_id;
        $where['type'] = $type;
        $where['channel'] = $channel;

        $count = $this->db->from("ml_share_log")->where($where)->count_all_results();
        return $count;
    }



    //图谱是总共只能分享1次
    public function share_all_count($user_id,$type,$share_id)
    {
        $where = [];
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $where['share_id'] = $share_id;
        $this->db->select();
        $count = $this->db->from("ml_share_log")->where($where)->count_all_results();
        return $count;
    }

    // 同1人 1病历 1渠道1次   1积分
    public function post_be_shared_count($user_id,$post_id,$channel)
    {
        $where = [];
        $where['user_id']   = $user_id;
        $where['share_id']   = $post_id;
        $where['type']      = 0;
        $where['channel']  = $channel;
        $this->db->select();
        $count = $this->db->from("ml_share_log")->where($where)->count_all_results();
        return $count;
    }


    //处理帖子分享
    public function deal_post($share_id,$uid,$type,$channel)
    {
        $this->load->model("postm");
        $this->load->model("newjfpointsm");
        $resource_name = "post";
        $begin_time = strtotime("today");
        $end_time = strtotime("tomorrow");
        $is_add_jfpoints = FALSE; //是否添加积分和发送系统消息

        $post_info = $this->postm->get_one_post_4_search($share_id, $this->uid);
        if($post_info)
        {
            $share_count = $this->share_cout_bydate($uid,$begin_time,$end_time,$type,$share_id);
            $share_count_channel = $this->share_cout_bydate_with_channel($uid,$begin_time,$end_time,$type,$share_id,$channel);
            //w_log('$share_count is '.$share_count);
            //w_log('$share_count_channel is '.$share_count_channel);
            $is_add_jfpoints = ($share_count <= 10 && $share_count_channel <=1) ? TRUE : FALSE;
        }
        //病例分享：取消站内分享、微信收藏、邮件分享积分
        if(!in_array($channel,array(4,7)))
        {
            //给分享的人加积分
            if($is_add_jfpoints)
            {
                $this->newjfpointsm->add_minusjf_sendmessage($uid,$share_id,SHARE_POST,$share_id,Newjfpointsm::ADD_TYPE);
            }

            //给作者加积分
            //病例被转发  同1人 1病历 1渠道1次   1积分
            $post_be_shared_count = $this->post_be_shared_count($uid,$share_id,$channel);
            if($post_be_shared_count == 1 && $uid != $post_info['author_id'])
            {
                $shared_result = $this->newjfpointsm->add_minusjf_sendmessage($post_info['author_id'],$share_id,POST_BE_SHARED,$share_id,Newjfpointsm::ADD_TYPE);

            }
        }


        return true;
    }

    //处理专题分享
    public function deal_disubject($share_id,$uid,$type,$channel)
    {
        $this->load->model("disubjectm");
        $this->load->model("newjfpointsm");
        $resource_name = "disubject";
        $begin_time = strtotime("today");
        $end_time = strtotime("tomorrow");
        $is_add_jfpoints = FALSE; //是否添加积分和发送系统消息
//w_log("分享渠道为 ".$channel);
        //在给分渠道内  才考虑积分的事
        if(in_array($channel,array('2','3','5','6')))
        {
            //msgtype 需要一个新类型3   不跳转
            $disubject_info = $this->disubjectm->get_one_disubject($share_id);
            if($disubject_info) {
                $share_img = $disubject_info['images_thumb'] ? $disubject_info['images_thumb'] : $disubject_info['small_img'];
                $share_count = $this->sharem->share_cout_bydate($uid, $begin_time, $end_time, $type, $share_id);
                $share_count_channel = $this->sharem->share_cout_bydate_with_channel($uid, $begin_time, $end_time, $type, $share_id, $channel);
                //w_log("分享次数".$share_count);
                //w_log("渠道分享次数".$share_count_channel);
                $is_add_jfpoints = ($share_count <= 5 && $share_count_channel <= 1) ? TRUE : FALSE;
            }
//w_log("是否可以添加".$is_add_jfpoints);
            if($is_add_jfpoints)
            {
                $share_title = "专题";
                //android没有传参数过来 暂时去掉分享专题给积分的问题
                $result = $this->newjfpointsm->add_jfpoints_for_one_user($uid,SHARE_DISUBJECT,$share_id,SHARE_DISUBJECT);
                if($result['code'] == 0){
                    //发消息  给了积分才能发消息
                    $user_info = $this->userm->getUserInfoById($uid);
                    $jfpoints_content = $this->newjfpointsm->get_jfpoints_content(SHARE_DISUBJECT);
                    $send_content = array("post_first_img"=>$share_img,"content"=>$jfpoints_content['stage']);
                    $send_content = json_encode($send_content);
                    $this->messagem->send_msg_basic(31495, $uid, $send_content,$apptype=2,$msgtype=3);//默认用轻盈医学官方账号发送
                }
            }
        }

        return true;
    }

    //处理图谱分享
    public function deal_part($share_id,$uid,$type,$channel)
    {
        $this->load->model("packagem");
        $this->load->model("newjfpointsm");
        $resource_name = "part";
        $is_add_jfpoints = FALSE; //是否添加积分和发送系统消息


            //msgtype 需要一个新类型3   不跳转  //如果是要获取图谱静态信息 请用图谱获取静态函数
            $package_info = $this->packagem->get_package_staic_content($share_id);
            //图谱有内容才能给积分
            if($package_info) {
                $share_count = $this->sharem->share_all_count($uid, $type, $share_id);
                //w_log('$share_count is '.$share_count);
                $is_add_jfpoints = $share_count <= 2 ? TRUE : FALSE;
            }


        if($is_add_jfpoints)
        {
            $share_title = "图谱";
            $result = $this->newjfpointsm->add_jfpoints_for_one_user($uid,SHARE_PART,$share_id,SHARE_PART);
            if($result['code'] == 0){
                //发消息  给了积分才能发消息
                $user_info = $this->userm->getUserInfoById($uid);
                $jfpoints_content = $this->newjfpointsm->get_jfpoints_content(SHARE_PART);
                $send_content = array("post_first_img"=>$package_info['atlas_cover'],"content"=>$jfpoints_content['stage']);
                $send_content = json_encode($send_content);
                $this->messagem->send_msg_basic(31495, $uid, $send_content,$apptype=2,$msgtype=3);//默认用轻盈医学官方账号发送
            }
        }

        return true;
    }
}